package cc.fs.zhihuapp.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.model.VideoBean;

/**
 * Created by fostion on 2015/11/3.
 */
public class DataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<VideoBean> list;
    private OnItemClickListener onItemClickListener = null;

    public DataAdapter(ArrayList<VideoBean> _list){
        list = _list;
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_theme, parent, false);
        return new ItemViewHolder(v);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof  ItemViewHolder){
            ItemViewHolder vh = (ItemViewHolder)holder;
            vh.setData(list.get(position));
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.title)
        TextView title;

        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);

        }

        public void setData(final VideoBean videoBean){
            title.setText(videoBean.getTitle());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(videoBean);
                    }
                }
            });
        }
    }

    public interface OnItemClickListener{
        void onItemClick(VideoBean videoBean);
    }

}
